package com.kharthick.pwGame.map;

import java.awt.MediaTracker;
import java.io.FileReader;
import java.util.List;
import org.xml.sax.InputSource;
import org.xml.sax.XMLReader;
import org.xml.sax.helpers.XMLReaderFactory;

import com.kharthick.pwGame.players.Player;

public class Environment {
	Map map;
	List<Player> players;
	
    public Environment(String mapName, int numOfPlay, int tilesize, MediaTracker tracker) {
    	MapParser handler = null;
    	try{
    	
    	XMLReader xr = XMLReaderFactory.createXMLReader();
    	handler = new MapParser(numOfPlay, tilesize, tracker);
    	
    	xr.setContentHandler(handler);
    	xr.setErrorHandler(handler);

				// Parse each file provided on the
				// command line.
    	
    	FileReader r = new FileReader(mapName);
    	xr.parse(new InputSource(r));
    	
    	map = handler.getMap();
    	players = handler.getPlayers();
    	
    	}
    	catch(Exception e){
    		System.out.println("There is a error in reading the map." +
    				"\nTo avoid errors, please use the map editor.");
    		System.out.print(e.getMessage());
    		//System.out.println(e);
    		//e.printStackTrace();
    		map = null;
    	}
    }
    
    public Map getMap(){
    	return map;
    }
    
    public List<Player> getPlayers(){
    	return players;
    }
}
